package com.chuqiyun.authorization.controller.user;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuqiyun.authorization.annotation.ApiCheck;
import com.chuqiyun.authorization.entity.Bill;
import com.chuqiyun.authorization.entity.User;
import com.chuqiyun.authorization.service.BillService;
import com.chuqiyun.authorization.service.UserService;
import com.chuqiyun.authorization.utils.ObjectUtil;
import com.chuqiyun.authorization.utils.ResponseResult;
import com.chuqiyun.authorization.utils.ServletUtil;
import com.chuqiyun.authorization.utils.exception.UnauthorizedException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author mryunqi
 * @date 2023/4/24
 */
@RestController
public class UserBillController {
    @Resource
    private BillService billService;

    @Resource
    private UserService userService;
    @Value("${config.secret}")
    private String secret;

    /**
    * @Author: mryunqi
    * @Description: 获取用户账单列表
    * @DateTime: 2023/4/25 21:32
    */
    @ApiCheck
    @GetMapping("/user/getBillList")
    public ResponseResult<Page<Bill>> getBillList(HttpServletRequest request,
                                                  @RequestParam(value = "page") int page,
                                                  @RequestParam(value = "size") int size) throws UnauthorizedException {
        User cookieUser = ServletUtil.getLoginMember(request,secret);
        assert cookieUser != null;
        User user = userService.getUser(cookieUser.getPhone());
        QueryWrapper<Bill> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_uuid",user.getUuid());
        return ResponseResult.ok(billService.getBillList(page,size,queryWrapper));
    }

    /**
     * @Author: mryunqi
     * @Description: 取消用户账单
     * @DateTime: 2023/5/29 1:11
     */
    @ApiCheck
    @GetMapping("/user/cancelBilling")
    public ResponseResult<String> cancelBilling(HttpServletRequest request, @RequestBody JSONObject body){
        User cookieUser = ServletUtil.getLoginMember(request,secret);
        assert cookieUser != null;
        User user = userService.getUser(cookieUser.getPhone());
        Long cancelId = body.getLong("id");
        Bill bill = billService.getById(cancelId);
        // 判断账单是否存在
        if (ObjectUtil.isNull(bill)){
            return ResponseResult.fail(ResponseResult.RespCode.INVALID_BILL);
        }
        // 判断账单是否与用户匹配
        if (!user.getUuid().equals(bill.getUserUuid())){
            return ResponseResult.fail(ResponseResult.RespCode.INVALID_BILL);
        }
        bill.setState(2);
        if (billService.updateById(bill)){
            return ResponseResult.ok("账单取消成功");
        }else {
            // 保存失败，500系统错误
            return ResponseResult.fail(ResponseResult.RespCode.SYSTEM_ERROR);
        }

    }
}
